﻿using System;
using System.Data.OleDb;
using System.Windows.Forms;

namespace Texpro
{
    public partial class FMAD101 : Form
    {

        public FMAD101()
        {
            InitializeComponent();
        }

        private void frm_user_Load(object sender, EventArgs e)
        {
            load_form();
        }

        private void clear_form()
        {
            txt_user_id.Text = String.Empty;
            txt_user_name.Text = String.Empty;
            lst_user_role.SelectedIndex = -1;
            txt_password.Text = String.Empty;
            txt_confirm_password.Text = String.Empty;
        }

        private void load_form()
        {
            lbl_form_title.Text = this.Text;
            PCPUB.m_fill_combo(lst_user_role, "USER_ROLE");
            clear_form();
            switch (PCPUB.m_entry_form_mode)
            {
                case "ADDNEW":
                    txt_user_id.Enabled = true;
                    txt_user_id.Focus();
                    break;
                case "EDIT":
                    txt_user_id.Enabled = false;
                    fill_form((PCPUB.m_record_id));
                    txt_user_name.Focus();
                    break;
            }
            
        }

        private bool save_form()
        {
            try
            {
                String strQuery = String.Empty;
                OleDbConnection sqlConn = new OleDbConnection();
                OleDbCommand sqlComm = new OleDbCommand();
                //
                String user_id = String.Empty;
                String user_name = String.Empty;
                String user_role_id = String.Empty;
                String user_password = String.Empty;
                //
                user_id = "'" + txt_user_id.Text.ToString() + "'";
                user_name = "'" + txt_user_name.Text.ToString() + "'";
                user_role_id = "'" + ((m_combo_item)lst_user_role.SelectedItem).combo_code.ToString() + "'";
                user_password = "'" + txt_password.Text.ToString() + "'";
                //
                strQuery = String.Empty;
                switch (PCPUB.m_entry_form_mode)
                {
                    case "ADDNEW":
                        strQuery += "INSERT INTO [tis_user] (";
                        strQuery += "[user_id],";
                        strQuery += "[user_name],";
                        strQuery += "[user_role_id],";
                        strQuery += "[user_password]";
                        strQuery += ") VALUES (";
                        strQuery += user_id + ",";
                        strQuery += user_name + ",";
                        strQuery += user_role_id + ",";
                        strQuery += user_password + ")";
                        break;
                    case "EDIT":
                        strQuery += " UPDATE [tis_user] SET ";
                        strQuery += " [user_name] = " + user_name + ",";
                        strQuery += " [user_role_id] = " + user_role_id + ",";
                        strQuery += " [user_password] = " + user_password;
                        strQuery += " WHERE [user_id] = " + user_id;
                        break;
                }
                //
                sqlConn.ConnectionString = PCPUB.m_oledb_connection.ToString();
                sqlConn.Open();
                sqlComm.Connection = sqlConn;
                sqlComm.CommandText = strQuery.ToString();
                sqlComm.ExecuteNonQuery();
                sqlConn.Close();
                //
                sqlComm.Dispose();
                sqlConn.Dispose();
                return true;
            }
            catch (Exception e)
            {
                PCMSG.Show(e.Message, "User", MessageBoxIcon.Error);
                return false;
            }
        }

        private bool ValidateFields()
        {
            if (String.IsNullOrEmpty(txt_user_id.Text))
            {
                PCMSG.Show("User Id field should not be empty.");
                txt_user_id.Focus();
                return false;
            }
            else if (String.IsNullOrEmpty(txt_user_name.Text))
            {
                PCMSG.Show("User Name field should not be empty.");
                txt_user_name.Focus();
                return false;
            }
            else if (lst_user_role.SelectedIndex == -1)
            {
                PCMSG.Show("User Role field should not be empty.");
                lst_user_role.Focus();
                return false;
            }
            else if (String.IsNullOrEmpty(txt_password.Text))
            {
                PCMSG.Show("Password field should not be empty.");
                txt_password.Focus();
                return false;
            }
            else if (String.IsNullOrEmpty(txt_confirm_password.Text))
            {
                PCMSG.Show("Confirm Password field should not be empty.");
                txt_confirm_password.Focus();
                return false;
            }
            else if (!txt_password.Text.Equals(txt_confirm_password.Text))
            {
                PCMSG.Show("Password and Confirm Password should be same.");
                txt_confirm_password.Focus();
                return false;
            }
            return true;
        }

        private void but_save_Click(object sender, EventArgs e)
        {
            if (ValidateFields() == true)
            {
                if (save_form() == true)
                {
                    PCMSG.Show("Record saved successfully.");
                    switch (PCPUB.m_entry_form_mode)
                    {
                        case "ADDNEW":
                            clear_form();
                            txt_user_id.Focus();
                            break;
                        case "EDIT":
                            this.Close();
                            break;
                    }
                    
                }
            }
        }

        private void but_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frm_user_KeyUp(object sender, KeyEventArgs e)
        {
            //if (e.KeyCode == Keys.Enter)
            //{
            //    e.Handled = false;
            //    SendKeys.Send("{TAB}");
            //}
        }

        private void fill_form(string p_id)
        {
            String strQuery = String.Empty;
            OleDbConnection sqlConn = new OleDbConnection();
            OleDbCommand sqlComm = new OleDbCommand();
            OleDbDataReader sqlRead;// = new OleDbDataReader();
            DateTime strReturn = DateTime.Now;
            //           
            sqlConn.ConnectionString = PCPUB.m_oledb_connection.ToString();
            sqlConn.Open();
            //
            strQuery = String.Empty;
            strQuery += " SELECT";
            strQuery += " [user_id],";
            strQuery += " [user_name],";
            strQuery += " [user_role_id],";
            strQuery += " [user_password]";
            strQuery += " FROM [tis_user]";
            strQuery += " WHERE [user_id] = '" + p_id + "'";
            //
            sqlComm.Connection = sqlConn;
            sqlComm.CommandText = strQuery.ToString();
            sqlRead = sqlComm.ExecuteReader();
            //
            if (sqlRead.Read())
            {
                txt_user_id.Text = sqlRead["user_id"].ToString();
                txt_user_name.Text = sqlRead["user_name"].ToString();
                lst_user_role.SelectedIndex = lst_user_role.FindStringExact(PCPUB.m_get_user_role_name(sqlRead["user_role_id"].ToString()));
                txt_password.Text = sqlRead["user_password"].ToString();
                txt_confirm_password.Text = sqlRead["user_password"].ToString();
            }
            //
            sqlRead.Close();
            sqlConn.Close();
            sqlRead.Dispose();
            sqlComm.Dispose();
            sqlConn.Dispose();
        }

        private void text_id_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsLetterOrDigit(e.KeyChar) || e.KeyChar == (char)Keys.Back))
            {
                e.Handled = true;
            }
        }

        private void text_gen_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsLetterOrDigit(e.KeyChar) || e.KeyChar == (char)Keys.Back || e.KeyChar == (char)Keys.Space || e.KeyChar == '.'))
            {
                e.Handled = true;
            }
        }

        private void text_int_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsDigit(e.KeyChar) || e.KeyChar == (char)Keys.Back))
            {
                e.Handled = true;
            }
        }

        private void text_dec_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsDigit(e.KeyChar) || e.KeyChar == (char)Keys.Back || e.KeyChar == '.'))
            {
                e.Handled = true;
            }
        }

    }

}
